package com.service.impl;

import com.mapper.UserMapper;
import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public User login(String code, String password) {
        return userMapper.login(code,password);
    }

    public User getUserByUid(Integer id) {
        return userMapper.getUserByUid(id);
    }

    public boolean updateUserByUid(User user) {
        int res = userMapper.updateUserByUid(user);
        if(res>0){
            return true;
        }else {
            return false;
        }
    }

    public boolean addUser(User user) {
        int res = userMapper.addUser(user);
        if(res>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<User> findUserList(String userCode, String userName, Integer userRole, Boolean userStatus,
                                   Integer pageIndex, Integer pageSize) {
        // 数据转换
        Map<String, Object> conditions=new HashMap<String, Object>();
        conditions.put("code", userCode);
        conditions.put("name", userName);
        conditions.put("role", userRole);
        conditions.put("status", userStatus);
        conditions.put("from", (pageIndex-1)*pageSize);// （页码-1）*页面容量
        conditions.put("size", pageSize);
        return userMapper.getUserList(conditions);
    }

    @Override
    @Transactional(readOnly = true)
    public Integer findUserCount(String userCode, String userName, Integer userRole, Boolean userStatus) {
        // 数据转换
        Map<String, Object> conditions=new HashMap<String, Object>();
        conditions.put("code", userCode);
        conditions.put("name", userName);
        conditions.put("role", userRole);
        conditions.put("status", userStatus);
        return userMapper.getUserCount(conditions);
    }

    public boolean deleteUserByUid(Integer uid) {
        int res = userMapper.deleteUserByUid(uid);
        if (res>0){
            return true;
        } else{
            return false;
        }

    }


}
